import 'dart:ui';

import 'package:flutter/material.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';
import 'package:wanAndroid/common/http/http_service.dart';
import 'package:wanAndroid/common/http/http_uts.dart';
import 'package:wanAndroid/common/uts/log_uts.dart';
import 'package:wanAndroid/common/uts/status_uts.dart';
import 'package:wanAndroid/common/uts/toast_uts.dart';
import 'package:wanAndroid/common/uts/ui_uts.dart';
import 'package:wanAndroid/page/page_about.dart';
import 'package:wanAndroid/page/page_about_me.dart';
import 'package:wanAndroid/page/page_login.dart';
import 'package:wanAndroid/page/page_my_collection.dart';
import 'package:wanAndroid/page/page_personal_points.dart';
import 'package:wanAndroid/res/colors.dart';

class PageMe extends StatefulWidget {
  @override
  State<StatefulWidget> createState() => _PageMe();
}

class _PageMe extends State<PageMe> {
  RefreshController _controller;

  /// 是否登录
  bool _isLogin = false;

  @override
  void initState() {
    _controller = RefreshController(initialRefresh: false);
    StatusUts.checkLogin().then((value) {
      LogUts.showLog(value, tag: "是否登录····");
      setState(() {
        this._isLogin = value;
      });
    });
    super.initState();
  }

  ///
  /// 检测死都登录
  void checkLogin(int pos) async {
    switch (pos) {
      case 0:
        StatusUts.checkLogin2Login(context).then((value) {
          //个人积分
          if (value) {
            PagePersonalPoints.start(context);
          }
        });
        break;
      case 1:
        StatusUts.checkLogin2Login(context).then((value) {
          //我的收藏
          if (value) {
            PageMyCollection.start(context);
          }
        });
        break;
      case 2:
        PageAboutMe.start(context);
        //关于作者
        break;
      case 3:
        PageAbout.start(context);
        //关于程序
        break;
      default:
        ToastUts.showToast(context, text: "敬请期待~~~");
        break;
    }
  }

  //刷新数据
  Future<void> onRefreshListener() async {
    StatusUts.checkLogin2Login(context).then((value) {
      LogUts.showLog(value, tag: "是否登录····");
      setState(() {
        this._isLogin = value;
      });
    });
    //这里是刷新
    Future.delayed(
        Duration(seconds: 2), () => {UiUts.finishRefresh(_controller)});
  }

  @override
  Widget build(BuildContext context) => Scaffold(
        appBar: UiUts.NoTitlebar(),
        backgroundColor: Color(0xfff1f1f1),
        body: SmartRefresher(
          controller: _controller,
          enablePullUp: false,
          enablePullDown: true,
          header: ClassicHeader(),
          onRefresh: onRefreshListener,
          child: ListView(
            children: [
              Container(
                height: 200,
                width: double.infinity,
                child: Stack(
                  children: [
                    Container(
                      width: double.infinity,
                      child: Image.asset(
                        'images/icon_user.jpeg',
                        fit: BoxFit.fitWidth,
                      ),
                    ),
                    BackdropFilter(
                      filter: ImageFilter.blur(sigmaX: 0, sigmaY: 0),
                      child: Opacity(
                        opacity: 0.9,
                        child: Container(
                          padding: const EdgeInsets.only(
                            left: 10,
                            right: 10,
                            top: 5,
                            bottom: 5,
                          ),
                          decoration: BoxDecoration(color: Color(0x99000000)),
                          child: Center(
                            child: ClipOval(
                              child: Image.asset(
                                'images/icon_user.jpeg',
                                width: 150,
                                height: 150,
                                fit: BoxFit.cover,
                              ),
                            ),
                          ),
                        ),
                      ),
                    ),
                    InkWell(
                      onTap: _isLogin
                          ? null
                          : () async {
                              _isLogin = await PageLogin.start(context);
                              onRefreshListener();
                            },
                      child: Container(
                        margin: const EdgeInsets.only(
                          left: 15,
                          top: 15,
                        ),
                        height: 30,
                        width: 80,
                        constraints: BoxConstraints(
                          minHeight: 30,
                          minWidth: 70,
                        ),
                        decoration: BoxDecoration(
                          color: _isLogin
                              ? Color(0x801d953f)
                              : ResColors.primaryColor,
                          borderRadius: BorderRadius.circular(
                            15,
                          ),
                        ),
                        alignment: Alignment.center,
                        child: UiUts.getText(
                          _isLogin ? "已登录" : "去登录",
                          color: Colors.white,
                          fontSize: 14,
                        ),
                      ),
                    )
                  ],
                ),
              ),
              UiUts.getDivider(height: 5),
              InkWell(
                child: Container(
                  height: 50,
                  padding: const EdgeInsets.symmetric(horizontal: 15),
                  decoration: BoxDecoration(color: Colors.white),
                  child: Row(
                    mainAxisSize: MainAxisSize.max,
                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                    crossAxisAlignment: CrossAxisAlignment.center,
                    children: [
                      UiUts.getText("个人积分", color: ResColors.primaryFontColor),
                      Icon(
                        Icons.chevron_right,
                        color: Colors.grey,
                      ),
                    ],
                  ),
                ),
                onTap: () => checkLogin(0),
              ),
              UiUts.getDivider(height: 5),
              InkWell(
                child: Container(
                  height: 50,
                  padding: const EdgeInsets.symmetric(horizontal: 15),
                  decoration: BoxDecoration(color: Colors.white),
                  child: Row(
                    mainAxisSize: MainAxisSize.max,
                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                    crossAxisAlignment: CrossAxisAlignment.center,
                    children: [
                      UiUts.getText("我的收藏", color: ResColors.primaryFontColor),
                      Icon(
                        Icons.chevron_right,
                        color: Colors.grey,
                      ),
                    ],
                  ),
                ),
                onTap: () => checkLogin(1),
              ),
              UiUts.getDivider(height: 5),
              InkWell(
                child: Container(
                  height: 50,
                  padding: const EdgeInsets.symmetric(horizontal: 15),
                  decoration: BoxDecoration(color: Colors.white),
                  child: Row(
                    mainAxisSize: MainAxisSize.max,
                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                    crossAxisAlignment: CrossAxisAlignment.center,
                    children: [
                      UiUts.getText("关于作者", color: ResColors.primaryFontColor),
                      Icon(
                        Icons.chevron_right,
                        color: Colors.grey,
                      ),
                    ],
                  ),
                ),
                onTap: () => checkLogin(2),
              ),
              UiUts.getDivider(height: 5),
              InkWell(
                child: Container(
                  height: 50,
                  padding: const EdgeInsets.symmetric(horizontal: 15),
                  decoration: BoxDecoration(color: Colors.white),
                  child: Row(
                    mainAxisSize: MainAxisSize.max,
                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                    crossAxisAlignment: CrossAxisAlignment.center,
                    children: [
                      UiUts.getText("关于程序", color: ResColors.primaryFontColor),
                      Icon(
                        Icons.chevron_right,
                        color: Colors.grey,
                      ),
                    ],
                  ),
                ),
                onTap: () => checkLogin(3),
              ),
            ],
          ),
        ),
      );

  ///  获取用户信息
  Future<void> getUserinfo() async {
    HttpService.userInfo(HttpCallback((rsp) {}, (e) {}));
  }
}
